package com.cqupt.controller;

import com.cqupt.pojo.ResponseMessage;
import com.cqupt.service.ActionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 主机管理相关请求
 **/
@CrossOrigin
@Controller
@RequestMapping("host_management")
@ResponseBody
public class HostManagementController {
    private final ActionService actionService;

    @Autowired
    public HostManagementController(ActionService actionService) {
        this.actionService = actionService;
    }

    /**
     * 向主机发送重启命令
     *
     * @param hostId   要重启的主机ID
     * @param user     要重启的主机账户
     * @param password 要重启的主机密码
     * @return 封装的返回消息，含result(命令返回消息)
     */
    @RequestMapping("/send_reboot_to_host")
    public ResponseMessage sendRebootToHost(@RequestParam(value = "hostId") String hostId,
                                             @RequestParam(value = "account") String user,
                                             @RequestParam(value = "password") String password
    ) {
        ResponseMessage responseMessage = new ResponseMessage();

        final Map<String, Object> resultMap = actionService.sendCommandToHost(1, hostId, user, password);
        responseMessage.setContent(resultMap);
        responseMessage.setStatus(true);

        return responseMessage;
    }

    /**
     * 向主机发送关机命令
     *
     * @param hostId   要关机的主机ID
     * @param user     要关机的主机账户
     * @param password 要关机的主机密码
     * @return 封装的返回消息，含result(命令返回消息)
     */
    @RequestMapping("/send_shutdown_to_host")
    public ResponseMessage sendShutdownToHost(@RequestParam(value = "hostId") String hostId,
                                               @RequestParam(value = "account") String user,
                                               @RequestParam(value = "password") String password
    ) {
        ResponseMessage responseMessage = new ResponseMessage();

        final Map<String, Object> resultMap = actionService.sendCommandToHost(0, hostId, user, password);
        responseMessage.setContent(resultMap);
        responseMessage.setStatus(true);

        return responseMessage;
    }

}
